<?php
//----------------------------------------------------------------------------------------------------------
    require_once('../config.inc.php');

    require_once($config['app_dir'] . '/classes/stockplot.view.php');
    require_once($config['app_dir'] . '/classes/stockplot.data.php');
    require_once($config['app_dir'] . '/classes/staff.class.php');
    //------------------------------------------------------------------------------------------------------

	$Staff = new objStaff($config['app_id']);
        if( !$Staff->Login()) exit;
    else {
    	if( $Staff->isTrialFinished() && !$Staff->checkSubscribed() ) exit;
    }

    //------------------------------------------------------------------------------------------------------
    try {
      //---------------------------------------------------------------------------------------------------
      // Purge old cached image
      //---------------------------------------------------------------------------------------------------
      $caching_period = 2;//in minutes

      $filename = basename(__FILE__);
      $d = dir(CACHE_DIR);
      while (false !== ($entry = $d->read()))
         if (is_file($d->path . $entry) &&
            0===strpos($entry, $filename) &&
            (strtotime('now') - filemtime($d->path . $entry)) > 2*$caching_period*60 ) unlink($d->path . $entry);

      $d->close();
      //---------------------------------------------------------------------------------------------------
      // checking input parameters
      //---------------------------------------------------------------------------------------------------
      $StockParams = new objStockParams();
      //---------------------------------------------------------------------------------------------------
      // preparing data for graph
      //---------------------------------------------------------------------------------------------------
      $StockView = new objStockView(649, 390, $caching_period); //caching 2 minutes
//      $StockView = new objStockView(500, 315, $caching_period); //caching 2 minutes
      //---------------------------------------------------------------------------------------------------
      if( !$StockView->is_cached ) {
         //$StockValidator = new objStockValidationPeriod($StockParams->trade,$StockParams->start,$StockParams->finish);
         //$StockValidator->isValid();

         $StockModel = new objStockModel($StockParams->trade,
                                         $StockParams->start,
                                         $StockParams->finish,
                                         $StockParams->t);
         $StockModel->StrongInterval(true);
         //------------------------------------------------------------------------------------------------
         $IDParam = new objIDParam();
         $id = $IDParam->id;
         if( !$id ) $id = $StockModel->GenID();
         //------------------------------------------------------------------------------------------------
         $data = $Staff->Session->get('forecasts_'.$id);
         $Staff->Session->clear('forecasts_'.$id);
         //------------------------------------------------------------------------------------------------
         if( empty($data) ) $data = $StockModel->getData();
         //------------------------------------------------------------------------------------------------
         // generating graph
         //------------------------------------------------------------------------------------------------
          $StockView->HideLogo();
          $StockView->HideCopyright();
          $StockView->HideNews();


         $StockView->SetData($data);
         //------------------------------------------------------------------------------------------------
         $StockView -> Render();
      } //if not cached
      //---------------------------------------------------------------------------------------------------
    } catch (Exception $e) {
        echo 'Error: ',  $e->getMessage(), "\n";
    }
   //-------------------------------------------------------------------------------------------------------
?>
